package com.example.smartcitydemo.repo.person

import android.util.Log
import com.example.smartcity_jscs.network.ResultStates.Resource
import com.example.smartcitydemo.base.BaseRepo
import com.example.smartcitydemo.bean.*
import com.example.smartcitydemo.exception.AppExcpetion
import com.example.smartcitydemo.network.client.authApiService
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import java.lang.RuntimeException

/**
 * @PACKAGE_NAME: com.example.smartcitydemo.repo.person
 * @NAME: Caleb.F
 * @DATE: 2021/11/11 8:38
 * @Email: 1919796283@qq.com
 */
object PersonRepo : BaseRepo() {
    suspend fun getPersonInfo(): Flow<Resource<User>> {
        return flow {
            val personInfo = authApiService.getPersonInfo()
            Log.d("dedef", "getPersonInfo: $personInfo")
            if (personInfo.code == 200 ) {
                //获取成功
                personInfo.user!!.run {
                    emit(Resource.Success(this))
                }
            } else {
                Log.d("deded", "getPersonInfo: ${personInfo.code}")
                handleResponseErrorCode(personInfo.code, personInfo.msg)
            }
        }.flowOn(Dispatchers.IO)
    }

    suspend fun modifyPersonInfo(modifyPersonInfo: ModifyPersonInfo): Flow<Resource<String>> {
        return requestCommonData { authApiService.modifyPersonInfo(modifyPersonInfo) }
    }

    suspend fun modifyPassword(modifyPassword: ModifyPassword): Flow<Resource<String>> {
        return requestCommonData { authApiService.modifyPassword(modifyPassword) }
    }

    suspend fun replyMessage(replyMessage: ReplyMessage): Flow<Resource<String>> {
        return requestCommonData { authApiService.doReplyMessage(replyMessage) }
    }

    suspend fun getReplyList(): Flow<Resource<List<ReplyBean>>> {
        return requestListData({ authApiService.getReplyList() }, { it })
    }
}